﻿var MyMRP = {
    version: '0.9.0',

    prefix: 'MRP_',

    printpage: function(myDiv) {
        //        var newstr = document.all.item(myDiv).innerHTML;
        //        var oldstr = document.body.innerHTML;
        //        document.body.innerHTML = newstr;
        //        window.print();
        //        document.body.innerHTML = oldstr;
        //        return false;
        var divtoprint = document.getElementById(myDiv);
        if (divtoprint) {
            divtoprint.focus();
            window.print();
        }
    },

    test: function() {
        alert("Just for testing!");
    },

    JAX: function(url, vars) {
        var rq = new XMLHttpRequest();
        rq.open('POST', url, false); //采用同步的方式
        rq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        rq.send(vars);
        if (rq.status == 200)
            return rq.responseText;
        else
            return "Error " + rq.status + ":" + rq.statusText;
    },

    check_BillNo: function(fID, fLabel) {
        var v_billno = document.getElementById(fID).value;
        var v_orderID = document.getElementById('EVOLU_OrderID').value;

        var prm = ["action=validation&tableID=MRP_ImportExport&fieldID=BillNo&value=", v_billno, "&orderid=", v_orderID].join('');
        return MyMRP.JAX("evolutility.aspx?", prm);
    },

    check_InStock: function(fID, fLabel) {
        //var qty = document.getElementById(fID);
        var v_ExportQuanlity = document.getElementById(fID).value;
        var v_MaterialID = document.getElementById("EVOLU_MaterialID").value;
        //        if (drp) {
        //            if (drp.length > 0) {
        //                v_MaterialID = drp.value;
        var prm = ["action=validation&tableID=MRP_ImportExport&fieldID=ExportQuanlity&value=", v_ExportQuanlity, "&materialid=", v_MaterialID].join('');
        return MyMRP.JAX("evolutility.aspx?", prm);
        //            }
        //        }
        if (v_MaterialID <= 0)
            return 'Check In Stock 系统错误！';
    },

    check_DailyReport: function(fID, fLabel) {
        var v_ProduceQuantity = document.getElementById("EVOLU_ProduceQuantity").value;

        var v_RejectQuantity = document.getElementById("EVOLU_RejectQuantity").value;
        if (document.getElementById("EVOLU_BatchID")) {
            //            if (drp.length > 0) {
            var v_BatchID = document.getElementById("EVOLU_BatchID").value;
            //                v_MaterialID = drp.value;
            var prm = ["action=validation&tableID=MRP_DailyReport&fieldID=ProduceQuantity_RejectQuantity&value=1&ProduceQuantity=", v_ProduceQuantity, "&RejectQuantity=", v_RejectQuantity, "&BatchID=", v_BatchID].join('');
            return MyMRP.JAX("evolutility.aspx?", prm);

        }


    },
    check_ProductImportQuantity: function(fID, fLabel) {
        var v_MaterialImportQuantity = document.getElementById(fID).value;
        var v_MaterialID = document.getElementById("EVOLU_MaterialID").value;
        var v_oldQuantity = 0;

        if (document.getElementById("EVOLU_Quantity_ov"))
            v_oldQuantity = document.getElementById("EVOLU_Quantity_ov").value;


        //var prm = ["action=validation&tableID=MRP_Material_Import&fieldName=",fID,"&Quantity",v_MaterialImportQuantity,"&MaterialID=", v_MaterialID, "&OrderID=", v_OrderID].join('');
        var prm = ["action=validation&tableID=MRP_Product_Import&fieldID=", fID, "&value=", v_MaterialImportQuantity, "&oldValue=", v_oldQuantity, "&MaterialID=", v_MaterialID].join('');

        return MyMRP.JAX("evolutility.aspx?", prm);



    },
    check_SignOffQuantity: function(fID, fLabel) {
        var v_SignOffQuantity = document.getElementById(fID).value;
        var v_SentQuantity = document.getElementById("EVOLU_SendOutQuantity").innerHTML;

        if (v_SignOffQuantity > v_SentQuantity)
            return "实收数量不能大于送货数量。";
        return "";


    },
    check_MaterialReturnQuantity: function(fID, fLabel) {
        var v_MaterialReturnQuantity = document.getElementById(fID).value;
        var v_MaterialID = '', v_OrderID = '';
        var v_ItemID = '';

        if (document.getElementById("EVOL_ItemID"))
            v_ItemID = document.getElementById("EVOL_ItemID").value;


        if ((v_ItemID == null) || (v_ItemID == '') || (v_ItemID == '0')) {

            v_MaterialID = document.getElementById("EVOLU_MaterialID").value;
            v_ItemID = '0';
            v_OrderID = document.getElementById("EVOLU_OrderID").value;
        }


        //var prm = ["action=validation&tableID=MRP_Material_Import&fieldName=",fID,"&Quantity",v_MaterialImportQuantity,"&MaterialID=", v_MaterialID, "&OrderID=", v_OrderID].join('');
        var prm = ["action=validation&tableID=MRP_Material_Return&fieldID=", fID, "&value=", v_MaterialReturnQuantity, "&ItemID=", v_ItemID, "&MaterialID=", v_MaterialID, "&OrderID=", v_OrderID].join('');

        return MyMRP.JAX("evolutility.aspx?", prm);
    },

    check_MaterialImportQuantity: function(fID, fLabel) {
        var v_MaterialImportQuantity = document.getElementById(fID).value;
        var v_MaterialID = '', v_OrderID = '';
        var v_ItemID = '';

        if (document.getElementById("EVOL_ItemID"))
            v_ItemID = document.getElementById("EVOL_ItemID").value;


        if ((v_ItemID == null) || (v_ItemID == '') || (v_ItemID == '0')) {

            v_MaterialID = document.getElementById("EVOLU_MaterialID").value;
            v_ItemID = '0';
            v_OrderID = document.getElementById("EVOLU_OrderID").value;
        }


        //var prm = ["action=validation&tableID=MRP_Material_Import&fieldName=",fID,"&Quantity",v_MaterialImportQuantity,"&MaterialID=", v_MaterialID, "&OrderID=", v_OrderID].join('');
        var prm = ["action=validation&tableID=MRP_Material_Import&fieldID=", fID, "&value=", v_MaterialImportQuantity, "&ItemID=", v_ItemID, "&MaterialID=", v_MaterialID, "&OrderID=", v_OrderID].join('');

        return MyMRP.JAX("evolutility.aspx?", prm);
    },
    //用于取得材料的采购信息
    change_MaterialImportFields: function() {
        var v_OrderID = document.getElementById("EVOLU_OrderID").value;
        var v_MaterialID = document.getElementById("EVOLU_MaterialID").value;
        if (v_MaterialID && v_OrderID) {
            var prm = ["action=JSdep&XMLFileName=MRP_Material_Import&fieldName=MaterialID&value=", v_MaterialID, "&OrderID=", v_OrderID].join('');

            EvoUI.AJAX("evolutility.aspx?", prm, function(r) {
                eval("var ret=" + r);
                var riq = document.getElementById("EVOLU_RemainImportQuantity");
                if (riq)
                    riq.innerHTML = ret.RemainImportQuantity;
                var pu = document.getElementById("EVOLU_PurchaseUnit");
                if (pu)
                    pu.innerHTML = ret.PurchaseUnit;
                var cr = document.getElementById("EVOLU_ConversionRatio");
                if (cr)
                    cr.innerHTML = ret.ConversionRatio;

            });
        }


    },
    //用于取得材料的采购信息
    change_MaterialExportReturnFields: function() {
        var v_OrderID = document.getElementById("EVOLU_OrderID").value;
        var v_MaterialID = document.getElementById("EVOLU_MaterialID").value;
        if (v_MaterialID && v_OrderID) {
            var prm = ["action=JSdep&XMLFileName=MRP_Material_Export_Return&fieldName=MaterialID&value=", v_MaterialID, "&OrderID=", v_OrderID].join('');

            EvoUI.AJAX("evolutility.aspx?", prm, function(r) {
                eval("var ret=" + r);
                var riq = document.getElementById("EVOLU_RemainImportQuantity");
                if (riq)
                    riq.innerHTML = ret.RemainImportQuantity;
                var pu = document.getElementById("EVOLU_PurchaseUnit");
                if (pu)
                    pu.innerHTML = ret.PurchaseUnit;
                var cr = document.getElementById("EVOLU_ConversionRatio");
                if (cr)
                    cr.innerHTML = ret.ConversionRatio;

            });
        }


    },
    //用于取得材料的单位信息并计算单重
    change_MaterialExportFields: function() {

        var v_MaterialID = 0;
        var v_QuantityOfStockUnit = 0.00000;
        if (document.getElementById("EVOLU_MaterialID"))
            v_MaterialID = document.getElementById("EVOLU_MaterialID").value;
        if (document.getElementById("EVOLU_QuantityOfStockUnit")) {
            v_QuantityOfStockUnit = document.getElementById("EVOLU_QuantityOfStockUnit").value;
            if ((v_QuantityOfStockUnit == '') || (v_QuantityOfStockUnit == 'undefined'))
                v_QuantityOfStockUnit = 0;
        }

        if (v_MaterialID > 0) {
            var prm = ["action=JSdep&XMLFileName=MRP_Material_Import&fieldName=MaterialIDQuantityOfStockUnit&value=", v_MaterialID, "&QuantityOfStockUnit=", v_QuantityOfStockUnit].join('');

            EvoUI.AJAX("evolutility.aspx?", prm, function(r) {
                eval("var ret=" + r);
                var pu = document.getElementById("EVOLU_PurchaseUnit");
                if (pu)
                    pu.innerHTML = ret.PurchaseUnit;
                var cr = document.getElementById("EVOLU_Weight");
                if (cr)
                    cr.innerHTML = ret.Weight;

            });
        }


    },
    newDeliveryItem: function() {
        var v_ProductIDCtl = document.getElementById("evolGFE2"); //取得产品的下拉列表

        if (v_ProductIDCtl && (!v_ProductIDCtl.onchange)) {

            v_ProductIDCtl.attachEvent('onchange', MyMRP.event_ProductID_onChange);
        }
    },
    event_ProductID_onChange: function() {

        var v_OrderIDCtl = document.getElementById("evolGFE1");
        var v_ProductIDCtl = document.getElementById("evolGFE2");
        if (v_OrderIDCtl && v_ProductIDCtl) {
            //MyMRP.test();
            var v_OrderID = v_OrderIDCtl.value;
            var v_ProductID = v_ProductIDCtl.value;
            if (v_OrderID && v_OrderID > 0 && v_ProductID && v_ProductID > 0) {
                v_SendOutQuantityCtl = document.getElementById("evolGFE3");
                if (v_SendOutQuantityCtl) {
                    var prm = ["action=JSdep&XMLFileName=MRP_DeliveryBill&fieldName=SendOutQuantity&OrderID=", v_OrderID, "&ProductID=", v_ProductID].join('');
                    //MyMRP.test();
                    var v_SendOutQuantity = MyMRP.JAX("evolutility.aspx?", prm);

                    v_SendOutQuantityCtl.value = v_SendOutQuantity;
                }

            }

        }


    },
    CheckSystem1: function() {
        var v_CheckSystemResult = document.getElementById("CheckSystemResult");
        if (v_CheckSystemResult) {
            v_CheckSystemResult.innerHTML = '正在检查系统，请不要关闭或者离开本页面....<br/>';
            MyMRP.CheckSystem('init');
        }
    },
    CheckSystem: function(fID) {
        var v_CheckSystemResult = document.getElementById("CheckSystemResult");
        var v_nextFID = '';


        //v_CheckSystemResult.innerHTML = '正在检查系统，请不要关闭或者离开本页面....<br/>';

        var prm = ["action=CheckSystem&fID=", fID].join('');

        EvoUI.AJAX("evolutility.aspx?", prm, function(r) {
            eval("var ret=" + r);
            v_CheckSystemResult.innerHTML += ret.msg;
            v_CheckSystemResult.innerHTML += '<br/>';
            v_nextFID = ret.fid;
            if ((v_nextFID) && (v_nextFID != '')) {
                MyMRP.CheckSystem(v_nextFID);
            } //else { v_CheckSystemResult.innerHTML += '检查结束！'; }
        });



    }


}
